"""
Case Type   : GUC
Case Name   : 使用set方式设置enable_opfusion为off，设置成功
Create At   : 2024/10/14
Owner       : nanyang12
Description :
        1.查询enable_opfusion默认值=on
        2.设置数据库级别，nable_opfusion=off;
        3.设置用户级别，enable_opfusion=off;
        4.设置会话级别，enable_opfusion=off;
        5.清理环境
Expect      :
        1.默认值"enable_opfusion=on"
        2.expect:下次会话中生效
        3.expect:下次会话中生效
        4.expect:当前会话中生效
        5.清理环境成功
History     :
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
commonsh = CommonSH('dbuser')


class DeveloperOptions(unittest.TestCase):
    def setUp(self):
        self.constant = Constant()
        self.user_node = Node('dbuser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        LOG.info(
            '------Opengauss_Function_DeveloperOptions_Case0119start----')

    def test_post_auth_delay(self):
        LOG.info('--步骤1:查询enable_opfusion默认值为on--')
        sql_cmd = commonsh.execut_db_sql('''show enable_opfusion;''')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.BOOLEAN_VALUES[0], sql_cmd)
        LOG.info('--step2:设置数据库级别，enable_opfusion=off;expect:下次会话中生效')
        LOG.info('--step2.1:设置enable_opfusion为off')
        sql_cmd = commonsh.execut_db_sql('''drop database if exists db_opfusion_0119; 
                    create database db_opfusion_0119;
                    alter database db_opfusion_0119 set enable_opfusion to off;
                    ''')
        LOG.info(sql_cmd)
        LOG.info('--重开会话查看参数,并恢复参数--')
        sql_cmd = commonsh.execut_db_sql('''
                    show enable_opfusion;
                    ''', dbname="db_opfusion_0119")
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''
                    alter database db_opfusion_0119 set enable_opfusion to on;
                    ''')
        self.assertIn("ALTER DATABASE", sql_cmd)

        LOG.info('--step2.2:设置enable_opfusion为false，同off')
        sql_cmd = commonsh.execut_db_sql('''
                    alter database db_opfusion_0119 set enable_opfusion to false;
                    ''')
        LOG.info(sql_cmd)
        LOG.info('--重开会话查看参数,并恢复参数--')
        sql_cmd = commonsh.execut_db_sql('''show enable_opfusion;
                    ''', dbname="db_opfusion_0119")
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''
                    alter database db_opfusion_0119 set enable_opfusion to true;
                    ''')
        self.assertIn("ALTER DATABASE", sql_cmd)

        LOG.info('--step2.3:设置enable_opfusion为no，同off')
        sql_cmd = commonsh.execut_db_sql('''
                    alter database db_opfusion_0119 set enable_opfusion to no;
                    ''')
        LOG.info(sql_cmd)
        LOG.info('--重开会话查看参数,并恢复参数--')
        sql_cmd = commonsh.execut_db_sql('''show enable_opfusion;
                    ''', dbname="db_opfusion_0119")
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''
                    alter database db_opfusion_0119 set enable_opfusion to yes;
                    ''')
        self.assertIn("ALTER DATABASE", sql_cmd)

        LOG.info('--step2.4:设置enable_opfusion为0，同off')
        sql_cmd = commonsh.execut_db_sql('''
                    alter database db_opfusion_0119 set enable_opfusion to 0;
                    ''')
        LOG.info(sql_cmd)
        LOG.info('--重开会话查看参数,并恢复参数--')
        sql_cmd = commonsh.execut_db_sql('''show enable_opfusion;
                    ''', dbname="db_opfusion_0119")
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''
                    alter database db_opfusion_0119 set enable_opfusion to 1;
                    ''')
        self.assertIn("ALTER DATABASE", sql_cmd)

        LOG.info('--step3:设置用户级别，enable_opfusion=off;expect:下次会话中生效')
        LOG.info('--step3.1:设置enable_opfusion为off')
        sql_cmd = commonsh.execut_db_sql('''drop user if exists u_opfusion_0119; 
                    create user u_opfusion_0119 with password 'test@123';
                    alter user u_opfusion_0119 set enable_opfusion to off;
                    ''')
        LOG.info(sql_cmd)
        LOG.info('--重开会话查看参数,并恢复参数--')
        connect = f'-d postgres -U u_opfusion_0119 -W test@123 -C'
        sql_cmd = commonsh.execut_db_sql('''
                    show enable_opfusion;
                    ''', sql_type=connect)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''
                    alter user u_opfusion_0119 set enable_opfusion to on;
                    ''')
        self.assertIn("ALTER ROLE", sql_cmd)

        LOG.info('--step3.2:设置enable_opfusion为false，同off')
        sql_cmd = commonsh.execut_db_sql('''
                    alter user u_opfusion_0119 set enable_opfusion to false;
                    ''')
        LOG.info(sql_cmd)
        LOG.info('--重开会话查看参数,并恢复参数--')
        connect = f'-d postgres -U u_opfusion_0119 -W test@123 -C'
        sql_cmd = commonsh.execut_db_sql('''
                    show enable_opfusion;
                    ''', sql_type=connect)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''
                    alter user u_opfusion_0119 set enable_opfusion to true;
                    ''')
        self.assertIn("ALTER ROLE", sql_cmd)

        LOG.info('--step3.3:设置enable_opfusion为no，同off')
        sql_cmd = commonsh.execut_db_sql('''
                    alter user u_opfusion_0119 set enable_opfusion to no;
                    ''')
        LOG.info(sql_cmd)
        LOG.info('--重开会话查看参数,并恢复参数--')
        connect = f'-d postgres -U u_opfusion_0119 -W test@123 -C'
        sql_cmd = commonsh.execut_db_sql('''
                    show enable_opfusion;
                    ''', sql_type=connect)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''
                    alter user u_opfusion_0119 set enable_opfusion to yes;
                    ''')
        self.assertIn("ALTER ROLE", sql_cmd)

        LOG.info('--step3.4:设置enable_opfusion为0，同off')
        sql_cmd = commonsh.execut_db_sql('''
                     alter user u_opfusion_0119 set enable_opfusion to 0;
                    ''')
        LOG.info(sql_cmd)
        LOG.info('--重开会话查看参数,并恢复参数--')
        connect = f'-d postgres -U u_opfusion_0119 -W test@123 -C'
        sql_cmd = commonsh.execut_db_sql('''
                    show enable_opfusion;
                    ''', sql_type=connect)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''
                     alter user u_opfusion_0119 set enable_opfusion to 1;
                    ''')
        self.assertIn("ALTER ROLE", sql_cmd)

        LOG.info('--step4:设置会话级别，enable_opfusion=off;expect:当前会话中生效')
        LOG.info('--step4.1:设置enable_opfusion为off')
        sql_cmd = commonsh.execut_db_sql('''set enable_opfusion to off; 
                    show enable_opfusion;
                    set enable_opfusion to on;
                    ''')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        self.assertIn("SET", sql_cmd)

        LOG.info('--step4.2:设置enable_opfusion为false，同off')
        sql_cmd = commonsh.execut_db_sql('''
                    set enable_opfusion to false;
                    show enable_opfusion;
                    set enable_opfusion to true;
                    ''')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        self.assertIn("SET", sql_cmd)

        LOG.info('--step4.3:设置enable_opfusion为no，同off')
        sql_cmd = commonsh.execut_db_sql('''
                    set enable_opfusion to no;
                    show enable_opfusion;
                    set enable_opfusion to yes;
                    ''')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        self.assertIn("SET", sql_cmd)

        LOG.info('--step4.4:设置enable_opfusion为0，同off')
        sql_cmd = commonsh.execut_db_sql('''
                     set enable_opfusion to 0;
                     show enable_opfusion;
                     set enable_opfusion to 1;
                    ''')
        LOG.info(sql_cmd)
        self.assertIn(self.constant.BOOLEAN_VALUES[1], sql_cmd)
        self.assertIn("SET", sql_cmd)


    def tearDown(self):
        LOG.info('--步骤3:清理环境--')
        sql_cmd = commonsh.execut_db_sql('''
                    drop database if exists db_opfusion_0119;
                    drop user if exists u_opfusion_0119;
                    ''')
        LOG.info(sql_cmd)
        LOG.info(
            '------Opengauss_Function_DeveloperOptions_Case0119finish----')
